package mumu.android.app;

import mumu.android.R;
import mumu.android.lib.Async.Future;
import mumu.android.lib.app.SysReceiver;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 广播接收处理，还在当前主线程中，耗时操作需要开异步线程。
 * 每次发送一个系统注册的广播，广播接收器会执行自己的构造函数。
 * 注意：同一个广播可以重复注册，这样系统中就存在多个广播接收器,即接收实例。
 * @author Administrator
 *
 */
public class BroadCast1 extends Activity {

	String action = "mumu.android.BroadCast.receiver2";
	SysReceiver sysReceiver;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("mumu.android.app.BroadCast1");
		setContentView(R.layout.broadcast1);

		Button btn_1 = (Button) findViewById(R.id.btn_1);// sendsms
		Button btn_2 = (Button) findViewById(R.id.btn_2);// send
		Button btn_3 = (Button) findViewById(R.id.btn_3);// reg
		Button btn_4 = (Button) findViewById(R.id.btn_4);// unreg

		btn_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction("android.provider.Telephony.SMS_RECEIVED");
				sendBroadcast(intent);
			}
		});
		btn_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction(action);
				sendBroadcast(intent);
			}
		});
		btn_3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sysReceiver = new SysReceiver();
				IntentFilter filter = new IntentFilter();
				filter.addAction(action);
				registerReceiver(sysReceiver, filter);
			}
		});
		btn_4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (sysReceiver != null)
					unregisterReceiver(sysReceiver);
			}
		});
	}

}
